import { SlateElement } from '@wangeditor/editor';
import { DavinciBlock } from './custom-types';

function parseHtml(elem: SlateElement): string {
	const { content = '', attrs, fontFamily, fontSize } = elem as DavinciBlock;
	let attr = '';
	if (attrs) {
		for (const key in attrs) {
			attr += `${key}="${attrs[key]}" `;
		}
	}
	let style = {
		'font-family': fontFamily,
		'font-size': fontSize
	};

	style = JSON.parse(JSON.stringify(style));
	let styleStr = '';
	for (const key in style) {
		styleStr += `${key}:${style[key]};`;
	}
	return `<span ${attr}${styleStr ? ' style="' + styleStr + '"' : ''}>${content}</span>`;
}

export const elemToHtmlConf = {
	type: 'davinci-block',
	elemToHtml: parseHtml
};
